
import {ajax,base,} from 'nc-lightapp-front';
import requestApi from '../requestApi';
import {headSumHandle} from './commonEvent';
import {updataFormData, updatacardTableData} from 'src/erm/sscrppublic/common/utils/changePageData';
import { updateBodyData,zzhllxLogic, setAllCardTableEditableKeys } from '../utils';
import _ from 'lodash-es'
function afterEvent(props,moduleId,key,value, changedrows,flag) {
    if (value == null) {
        if (moduleId == 'mtapp_detail' && key == 'orig_amount') {
            props.cardTable.setValByKeyAndRowId(moduleId, changedrows.rowid, key, { value: 0.0, display: 0.0 });
            value = { value: 0.0, display: 0.0 };
        } else {
            return; //值未改变时，直接返回不处理变更
        }
    }
    // if (value.value != null && value.value != undefined && value.value === changedrows[0].values[key].value) {
    if (value.value != null && value.value != undefined && value.value === changedrows.value) { //客户 客户版本联动报错
        return; //值未改变时，直接返回不处理变更
    }
    console.log('编辑后事件',value.value, changedrows.value);
    //触发参照过滤重算
    let allData = props.createMasterChildData(window.presetVar.pageId, window.presetVar.head.head1, window.presetVar.body.body1);
    let headValues = allData.head[presetVar.head.head1].rows[0].values;
    window.presetVar.headValuesAll = headValues;
    //requestApi.handleLoadDataResDada(props.meta.getMeta(), {data:allData});
    
    switch(moduleId) {
        case 'head' :
            if (key === 'pk_org') {
				if(!value||!value.value){
					
					props.form.setFormItemsValue(presetVar.head.head1,{'pk_org': changedrows})
					return
				}
			}
			if (key === 'pk_org_v') {
				if(!value||!value.value){
					
					props.form.setFormItemsValue(presetVar.head.head1,{'pk_org_v': changedrows})
					return
				}
			}
            switch(key) {
               
                case 'pk_org': //组织
                case 'billdate': //单据日期
                case 'apply_org': //申请单位
                case 'org_currinfo'://汇率
                case 'apply_dept': //申请部门
                case 'pk_iobsclass': //收支项目
                case 'billmaker': //申请人
                case 'pk_currtype': //币种
                case "zzhllyrq"://组织汇率来源日期
                case "zzhllx"://组织汇率类型
                case "pk_customer":
                case "pk_customer_v":
                case "pk_supplier"://表头供应商
                case "pk_supplier_v"://表头供应商
                case "reason": // 事由

                   

                    //表头修改后数据后台需要的结构， 一主一子  一主多子
                    let headBodyData = props.createHeadAfterEventData(window.presetVar.pageCode, presetVar.head.head1, presetVar.bodys, moduleId, key, value);
                    headBodyData.templateid = props.meta.getMeta().pageid;
                    headBodyData.tableid = moduleId
                    
                    requestApi.changeByHead({
                        data: headBodyData,
                        success: (data)=> {
                            props.beforeUpdatePage();
                            //设置表头数据
                            updataFormData(props, presetVar.head.head1, data.data.head[presetVar.head.head1]);
                            
                            updateBodyData(props, data.data.bodys)
                            
                            // BTM 性能优化，变更页面数据更新方法
                            props.updatePage(presetVar.head.head1, [moduleId]);
                            // 本币汇率字段key:是org_currinfo不是bbhl
                            let org_currinfo = props.form.getFormItemsValue(presetVar.head.head1, 'org_currinfo');
                            if (key == 'pk_currtype' && parseFloat(org_currinfo.value) == parseFloat(1)) {
                                props.form.setFormItemsDisabled(presetVar.head.head1, { org_currinfo: true });
                            } else {
                                props.form.setFormItemsDisabled(presetVar.head.head1, { org_currinfo: false });
                            }
                            setAllCardTableEditableKeys(props, data.data, key)

                        }
                    })
                    // pk_currtype：币种，org_currinfo：本币汇率
                    let currentrow_bzbm = props.form.getFormItemsValue(presetVar.head.head1,'pk_currtype');
                    // 新增汇率适配逻辑
                    let rate_category =  props.form.getFormItemsValue(presetVar.head.head1,'zzhllx.rate_category'); 
                    zzhllxLogic(currentrow_bzbm,rate_category,'head',props,'org_currinfo')

                    break;
            }
               //bugfix 表头字段编辑后，需要重新设置一下表体的收款对象相关字段的可编辑性
             //编辑后事件控制字段可编辑性需要放到valuechange之后
             let bbhlFlag = props.form.getFormItemsDisabled('head', 'org_currinfo'); //表头本币汇率编辑性
             let zzhllxFlag = props.form.getFormItemsDisabled('head', 'zzhllx'); //表头本币汇率类型编辑性
             let zzhllyrqFlag = props.form.getFormItemsDisabled('head', 'zzhllyrq'); //表头本币汇率日期编辑性

             presetVar.bodys.forEach((tablecode)=>{
                let allrows = props.cardTable.getAllRows(tablecode)
                if (allrows){
                    allrows.forEach((row) => {
                        // 表头中本币种、汇率类型更改后，表体中本币汇率、汇率类型、汇率来源日期需同时控制其编辑性
                        let editkeys = []
                        let unedikeys = []
                        bbhlFlag ? unedikeys.push('org_currinfo') : editkeys.push('org_currinfo')
                        zzhllxFlag ? unedikeys.push('zzhllx') : editkeys.push('zzhllx')
                        zzhllyrqFlag ? unedikeys.push('zzhllyrq') : editkeys.push('zzhllyrq')
                        props.cardTable.setEditableByRowId(tablecode, row.rowid, editkeys, true)
                        props.cardTable.setEditableByRowId(tablecode, row.rowid, unedikeys, false)
                    })
                }
                
             })  


            break;
        default:
            //满足报销标准核心控制项字段影响因素
            if(key!=window.presetVar.showitemkey 
                && ( (window.presetVar.reimsrc!=null && window.presetVar.reimsrc.length>0 && window.presetVar.reimsrc.includes(key))
                ||  (window.presetVar.reimruleitemkeys!=null && window.presetVar.reimruleitemkeys.length>0 && window.presetVar.reimruleitemkeys.indexOf(key) != -1) )
            ) {
                //清空当前行报销标准的值，以重新获取
                props.cardTable.setValByKeyAndRowId(presetVar.body.body1,changedrows[0].rowid,window.presetVar.showitemkey,{value:0.00, display: 0.00});

                let headBodyData = props.createBodyAfterEventData(window.presetVar.pageCode, presetVar.head.head1, presetVar.bodys,  moduleId, key, changedrows)
                let data={
                    ...headBodyData,
                    checkrule:'true',
                    templateid: props.meta.getMeta().pageid,
                    tableid: moduleId
                }
                requestApi.changeByBody({
                    data: data,
                    success: (data)=> {
                        //设置表头数据
                        props.beforeUpdatePage();
                        updataFormData(props, presetVar.head.head1, data.data.head[presetVar.head.head1]);
                        //设置表体数据
                        updateBodyData(props, data.data.bodys)
                        props.updatePage(presetVar.head.head1, [moduleId]);
                        // BTM 性能优化，变更页面数据更新方法
                    }
                })
                // 币种更改
                let currentrow_bzbm = props.cardTable.getValByKeyAndRowId(moduleId, changedrows[0].rowid, 'pk_currtype')
                let rate_category = props.cardTable.getValByKeyAndRowId(moduleId, changedrows[0].rowid, 'zzhllx.rate_category')
                zzhllxLogic(currentrow_bzbm,rate_category,'body',props,'org_currinfo',moduleId,changedrows[0].rowid)
                return;//满足报销标准核心控制项字段影响因素的字段直接返回
            }
            switch (key) {
                case 'orig_amount' :
                case 'org_currinfo' :
                case 'assume_org' :
                case 'assume_dept' :
                case 'pk_currtype' :
                case "zzhllyrq"://组织汇率来源日期
                case "zzhllx"://组织汇率类型
                case "pk_customer":
                case "pk_customer_v":
                case "pk_supplier"://表头供应商
                case "pk_supplier_v"://表头供应商

                default:
                    // let clickRow = props.cardTable.getClickRowIndex(moduleId)
                    let rowIndex = window.presetVar.clickRowIndex
                    if (key == "pk_customer_v" || key == "pk_customer") {
                        if (changedrows[0].newvalue.value !== changedrows[0].oldvalue.value) {
                            props.cardTable.setValByKeyAndIndex(moduleId, rowIndex,'pk_supplier', {value:'',display:''});
                            props.cardTable.setValByKeyAndIndex(moduleId, rowIndex,'pk_supplier_v', {value:'',display:''});
                        }
                    }
                    if (key == "pk_supplier_v" || key == "pk_supplier") {
                        if (changedrows[0].newvalue.value !== changedrows[0].oldvalue.value) {
                            props.cardTable.setValByKeyAndIndex(moduleId, rowIndex,'pk_customer', {value:'',display:''});
                            props.cardTable.setValByKeyAndIndex(moduleId, rowIndex,'pk_customer_v', {value:'',display:''});
                        }
                    }
                    let headBodyData = props.createBodyAfterEventData(window.presetVar.pageCode, presetVar.head.head1, presetVar.bodys, moduleId, key, changedrows)
                    headBodyData.templateid = props.meta.getMeta().pageid;
                    headBodyData.tableid = moduleId
                    requestApi.changeByBody({
                        data: headBodyData,
                        success: (data)=> {
                            props.beforeUpdatePage();
                            //设置表头数据
                            updataFormData(props, presetVar.head.head1, data.data.head[presetVar.head.head1]);
                            //设置表体数据
                            updateBodyData(props, data.data.bodys)
                            props.updatePage(presetVar.head.head1, [moduleId]);
                        }
                    })

                    // 编辑后，本币汇率、本币汇率日期、本币汇率来源日期编辑性设置
                    // 新增汇率适配逻辑
                    let currentrow_bzbm = props.cardTable.getValByKeyAndRowId(moduleId, changedrows[0].rowid, 'pk_currtype')
                    let rate_category = props.cardTable.getValByKeyAndRowId(moduleId, changedrows[0].rowid, 'zzhllx.rate_category')
                    zzhllxLogic(currentrow_bzbm,rate_category,'body',props,'org_currinfo',moduleId,changedrows[0].rowid)
            }
            
    }
}
export default afterEvent;